﻿//课程管理


function EditCurriculum(id) {
    
    
        layer.open({
            id: 1,
            type: 1,
            title: '编辑课程',
            area: ['500px', 'auto'],

            content: ' <div class="row" style="width: 420px;  margin-left:7px; margin-top:10px;">' +
                '<div class="col-sm-12">' +
                '<div class="input-group">' +
                '<span class="input-group-addon" style="background:white;border:none"> 课程名称:</span>' +
                '<input id="curriName" type="text" class="form-control" placeholder="请输入课程名称">' +
                '</div>' +
                '</div>' +
                '<div class="col-sm-12" style="margin-top: 10px">' +
                '<div class="input-group">' +
                '<span class="input-group-addon" style="background:white;border:none">选择学期:</span>' +
                '<select id="semester1" class="form-control" style="width:120px"></select>' +
                '<select id="semester2" class="form-control" style="width:120px;margin-left:10px">' +
                '<option value="1">第一学期</option>' +
                '<option value="2">第二学期</option>' +
                '</select>' +
                '</div>' +
                '</div>' +
                '</div>',
            btn: ['保存', '取消'],
            btn1: function(index, layero) {
                var curri = $("#curriName").val();
                var semester1 = $("#semester1 option:selected").val();
                var semester2 = $("#semester2 option:selected").val();
                //对比
                var oldCurriculaName = $(`#curriName_${id}`).text().trim();
                var oldYear = $(`#year_${id}`).text().trim();
                var oldSemester = $(`#semester_${id}`).text().trim();
                var temp;
                if (semester2 === "1") {
                    temp = "第一学期";
                } else {
                    temp = "第二学期";
                }
                if (curri === oldCurriculaName && semester1 === oldYear && temp === oldSemester) {
                    layer.close(index);
                    return false;
                }
                var loading = layer.msg('保存中',
                    {
                        icon: 16,
                        time: 5000,
                        shade: 0.1
                    });
                $.ajax({
                    type: "POST",
                    url: "/Curricula/EditCurriculum",
                    data: {
                        curriName: curri,
                        year: semester1,
                        semester: semester2,
                        Id: id
                    },
                    dataType: "json",

                    success: function(data) {
                        //layer.msg(data);
                        layer.close(loading);
                        layer.close(index);
                        if (data.tf === true) {
                            layer.msg(data.msg, { time: 500 }, function() { window.location.reload() });
                        } else
                            layer.msg(data.msg);
                    }
                });
            },
            btn2: function(index, layero) {
                layer.close(index);
            }
        });

    
        var a = $(`#curriName_${id}`).text();
        var c = $(`#year_${id}`).text().trim();
        var d = $(`#semester_${id}`).text().trim();
        $("#curriName").attr("value", a);
        var myDate = new Date();
        var year = myDate.getFullYear() + 1;
        for (var i = 0; i < 5; i++) {
        $("#semester1").append(`<option value='${(year - i - 1) + "-" + (year - i)}'>${(year - i - 1) + "-" + (year - i)}</option>`);
        }
        $(`#semester1 option[value='${c}']`).attr("selected", true);
        var temp = 1;
        if (d === "第二学期") {
        temp = 2;
        }
        $(`#semester2 option[value='${temp}']`).attr("selected", true);

}
function AddCurriculum() {
    layer.open({
        id: 1,
        type: 1,
        title: "新建课程",
        area: ["500px", "auto"],

        content: ' <div class="row" style="width: 420px;  margin-left:7px; margin-top:10px;">' +
            '<div class="col-sm-12">' +
            '<div class="input-group">' +
            '<span class="input-group-addon" style="background:white;border:none"> 课程名称:</span>' +
            '<input id="curriName" type="text" class="form-control" placeholder="请输入课程名称">' +
            '</div>' +
            '</div>' +
            '<div class="col-sm-12" style="margin-top: 10px">' +
            '<div class="input-group">' +
            '<span class="input-group-addon" style="background:white;border:none">选择学期:</span>' +
            '<select id="semester" class="form-control" style="width:120px"></select>' +
            '<select id="semester2" class="form-control" style="width:120px;margin-left:10px">' +
            '<option value="1">第一学期</option>' +
            '<option value="2">第二学期</option>' +
            '</select>' +
            '</div>' +
            '</div>' +
            '</div>',
        btn: ['创建', '取消'],
        btn1: function(index, layero) {
            var curri = $("#curriName").val();
            var semester1 = $("#semester option:selected").val();
            var semester2 = $("#semester2 option:selected").val();
            //layer.msg(curri + cl + semester + semester2);
            var loading = layer.msg('加载中',
                {
                    icon: 16,
                    time: 5000,
                    shade: 0.1
                });
            $.ajax({
                type: "POST",
                url: "/Curricula/AddCurriculum",
                data: {
                    curriName: curri,
                    year: semester1,
                    semester: semester2
                },
                dataType: "json",
                success: function(data2) {
                    layer.close(loading);
                    layer.close(index);
                    if (data2.tf === true) {
                        window.location.reload();
                    } else
                        layer.msg(data2.msg);

                },
                error: function() {
                    layer.msg("未知错误");
                }
            });
        },
        btn2: function (index, layero) {
            
            layer.close(index);
        }
       

});
    
    var myDate = new Date();
    var year = myDate.getFullYear() + 1;
    for (var i = 0; i < 5; i++) {
    $("#semester").append(`<option value='${(year - i - 1) + "-" + (year - i)}'>${(year - i - 1) + "-" + (year - i)}</option>`);
    }


}

function trigger(id) {
    var name = $(`#curriName_${id}`).text();
    layui.use('dropdown', function () {
        var dropdown = layui.dropdown
            , $ = layui.jquery;
        dropdown.render({
            elem: `#btnList_${id}`
            , show: true
            , data: [
                {
                    title: "编辑"
                    ,id: "edit"
                },
                {
                    title: "删除"
                    ,id: "del"
            }]
            , click: function (data, othis) {
                var elem = $(this.elem)
                    ,listId = elem.data('id'); //表格列表的预埋数据
                if (data.id === "edit") {
                    EditCurriculum(id);
                } else if (data.id === "del") {
                    layer.confirm('确认删除？', { icon: 3, title: '提示' }, function (index) {
                        $.ajax({
                            type: "POST",
                            url: "/Curricula/DeleteCurriculum",
                            data: {
                                curriculumId: id,
                                curriculumName: name
                            },
                            dataType: "json",
                            success: function (res) {

                                if (res.tf === true) {
                                    layer.msg(res.msg, { time: 2000 });
                                    $(`#f_${id}`).remove();
                                } else
                                    layer.msg(res.msg);

                            }, error: function () {
                                layer.msg("未知错误");
                            }
                        });
                        layer.close(index);
                    });
                }

            }
        });
    });
}
//班级函数

function OpenClass(e) {

    layer.open({
        id: 1,
        type: 1,
        title: '选择班级',
        area: ['40%', '30%'],
        content: ' <div class="row" style="width: 98%;  margin-left:10px; margin-top:10px;" id="Content2">'
            + '<div></div>'
            + '</div>',
        btn: ["进入", "修改", '删除', "关闭"],
        btn1: function (index, layero) {

            const text = $("[class='col-sm-2 layui-btn layui-btn-normal']").text();
            if (text !== "") {
                $('#linkToClass').attr('href', `/Curricula/Details/${e}/${text}`);

                document.getElementById('linkToClass').click();
                //window.location.href = 'Details/' + e + '/' + text;
            } else {
                layer.msg("请选择班级！");
            }
        },
        btn2: function (index, layero) {
            var oldClass = $("[class='col-sm-2 layui-btn layui-btn-normal']").text();
            if (oldClass !== "") {
                layer.prompt({ title: '修改名称', formType: 3 },
                    function (pass, index) {
                        layer.close(index);

                        $.ajax({
                            type: "POST",
                            url: "/Curricula/EditClasses",
                            data: {
                                curriculumId: e,
                                newName: pass,
                                oldName: oldClass
                            },
                            dataType: 'json',
                            success: function (data) {
                                layer.msg(data.msg);
                                if (data.tf === true) {
                                    $("[class='col-sm-2 layui-btn layui-btn-normal']").text(pass);
                                }
                            },
                            error: function (data) {
                                layer.msg(data.msg);
                            }

                        });
                    });
            } else {
                layer.msg("请选择班级！");
            }
            return false;
        },
        btn3: function (index, layero) {
            deleteClass(e);
            return false;
        },
        btn4: function (index, layero) {
            layer.close(index);
        }
    });
    $(".layui-layer-btn .layui-layer-btn1").attr("style", "background-color: orange;color: white");
    $(".layui-layer-btn .layui-layer-btn2").attr("style", "background-color: orangered;color: white");
    findClass(e);
}
function deleteClass(e) {
    var name = $("[class='col-sm-2 layui-btn layui-btn-normal']").text();
    if (name !== "") {
        layer.confirm('确定删除？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: "POST",
                url: "/Curricula/DeleteClasses",
                data: {
                    curriculumId: e,
                    className: name
                },
                dataType: 'json',
                success: function (data) {
                    layer.msg(data.msg);
                    if (data.tf === true) {
                        findClass(e);
                    }
                },
                error: function (data) {
                    layer.msg(data.msg);
                }
            });
        }, function () {


        });
    } else {
        layer.msg("请选择班级！");
    }
}
function findClass(e) {
    var htmlText = "";
    var indexFirst = layer.load(1, {
        time: 10 * 1000,
        shade: false //0.1透明度的白色背景
    });
    $.ajax({
        type: "POST",
        url: "/Curricula/FindClasses",
        data: {
            curriculumId: e
        },
        dataType: 'json',
        success: function (data) {
            $('#Content2').empty(); //初始化内容
            if (data.msg !== false) {
                var a = eval('(' + data + ')'); //转json对象
                //layer.msg(a[0]);
                for (let i in a) {
                    if (Object.prototype.hasOwnProperty.call(a, i)) {
                        htmlText += `<button name='classOp' class='col-sm-2 layui-btn layui-btn-primary' style='margin:8px ' onclick='onclickBtn(${i
                            })' id='btn${i}' >${a[i].ClassName}</button>`;
                    }
                }
                htmlText += `<button  class="col-sm-2 layui-btn layui-btn-primary" style="margin: 8px;color:gray" onclick="OpenPrompt(${e
                    })"><span class="layui-icon layui-icon-addition" style="font-size:30px;color:lightgray"></span></button>`;
                $('#Content2').append(htmlText);
            } else {
                htmlText += `<button  class="col-sm-2 layui-btn layui-btn-primary" style="margin: 8px;color:gray" onclick="OpenPrompt(${e
                    })"><span class="layui-icon layui-icon-addition" style="font-size:30px;color:lightgray"></span></button>`;
                $('#Content2').append(htmlText);
            }
            layer.close(indexFirst);

        }, error: function (data) {
            layer.msg(data.msg);
            layer.close(indexFirst);
        }
    });
}
function onclickBtn(e) {
    $("button[name*='classOp']").attr("class", "col-sm-2 layui-btn layui-btn-primary");
    $(`#btn${e}`).attr("class", "col-sm-2 layui-btn layui-btn-normal");
}
function OpenPrompt(e) {
    layer.prompt({ title: '班级名称', formType: 3 }, function (pass, index) {
        layer.close(index);
        $.ajax({
            type: "POST",
            url: "/Curricula/AddClasses",
            data: {
                curriculumId: e,
                className: pass
            },
            dataType: 'json',
            success: function (data) {
                layer.msg(data.msg);
                if (data.tf === true) {
                    findClass(e);
                }
            },
            error: function (data) {
                layer.msg(data.msg);
                return data.tf;
            }

        });
    });
}